package pers.qianyu.month_202102.date_20210219;

/**
 * 1004. 最大连续1的个数 III
 * https://leetcode-cn.com/problems/max-consecutive-ones-iii/
 *
 * @author mizzle rain
 * @date 2021-02-19 9:26
 */
public class LongestOnes {
    public int longestOnes(int[] A, int K) {
        int cnt1 = 0, cnt0 = 0;
        int n = A.length, res = 0;
        for (int left = 0, right = 0; right < n; right++) {
            if (A[right] == 1) cnt1++;
            else if (A[right] == 0) cnt0++;
            while (cnt0 > K) {
                if (A[left] == 1) cnt1--;
                else if (A[left] == 0) cnt0--;
                left++;
            }
            res = Math.max(res, cnt1 + cnt0);
        }
        return res;
    }
}
